//
//  MyCheckBiz.m
//  ZITHER
//
//  Created by weixin on 2018/11/23.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MyCheckBiz.h"
#import "AcceptPriceListModel.h"

@implementation MyCheckBiz


+ (instancetype)defaultManager {
    
    static MyCheckBiz *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[MyCheckBiz alloc]initWithFrame:CGRectMake(0, kScreenH, kScreenW, kScreenH)];
    });
    return manager;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        @weakify(self);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            [self disappearView];
        }];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:tap];
        self.backgroundColor = [UIColor whiteColor];
        
        
        [self addSubview:self.BizeScr];
        [self addSubview:self.BziInfo];
        [self addSubview:self.btn];
        
        [self.BizeScr mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.bottom.equalTo(self.btn.mas_top).offset(0);
            make.top.equalTo(self.BziInfo.mas_bottom).offset(10);
            make.right.equalTo(self).offset(-15);
            make.left.equalTo(self).offset(15);
        }];
        
        [self.BziInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.top.equalTo(self).offset(kStatusBarHeight+20);
            make.right.equalTo(self).offset(-15);
            make.left.equalTo(self).offset(15);
            make.height.width.mas_equalTo(kScreenW/350.0*444);
        }];
        
        [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.right.equalTo(self).offset(0);
            make.left.equalTo(self).offset(0);
            make.bottom.equalTo(self).offset(0);
            make.height.width.mas_equalTo(50);
        }];
        
    }
    return self;
}

-(void)disappearView{
    
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    [currentVC.tabBarController.tabBar setHidden:NO];
    
    [UIView animateWithDuration:0.1 animations:^{
        self.frame = CGRectMake(0, -20, kScreenW, kScreenH);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.2 animations:^{
            self.frame = CGRectMake(0, kScreenH, kScreenW, kScreenH);
        } completion:^(BOOL finished) {

        }];
    }];
}


-(void)cleanData{
//    self.textView.text = @"";
//    [self.viewImage cleanData];
}

-(BOOL)iseqHidden{
    if (DEFAULReleaseV.frame.origin.y==0) {
        NSLog(@"显示");
        return NO;
    }
    NSLog(@"");
    return YES;
}

-(BaseBtn *)btn{
    
    if (!_btn) {
        
        _btn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 0, 0) NormalText:@"关闭" NormalTextColor:[UIColor blackColor] SeleText:@"关闭" SeleTextColor:[UIColor blackColor] font:15 isBold:NO];
        [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _btn.backgroundColor = [UIColor whiteColor];
        @weakify(self);
        [[_btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self disappearView];
        }];
        
  
    }
    return _btn;
}
-(MyBizeScrVIew *)BizeScr{
    if (!_BizeScr) {
        _BizeScr = [[MyBizeScrVIew alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        @weakify(self);
        [_BizeScr setBlock:^(NSInteger index) {
            @strongify(self);
            self.BziInfo.model = self.arrM[index];
        }];
    }
    return _BizeScr;
}
-(MyBziInfoV *)BziInfo{
    if (!_BziInfo) {
        _BziInfo = [[MyBziInfoV alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _BziInfo.backgroundColor = [UIColor whiteColor];
    }
    return _BziInfo;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}


-(void)loadData{
    [[serviceDefualt loadGetAcceptPriceList:self.modeTyep1.userEnquiryInfo.goodsId page:0 reponseObjctClass:nil]subscribeNext:^(ResponseData* x) {
        if (x.success) {
            
            NSArray *arr = x.responseRawData[@"data"];
            NSMutableArray *arrM = [[NSMutableArray alloc]init];
            NSMutableArray *arrMImage = [[NSMutableArray alloc]init];

            for (NSDictionary *dic in arr) {
                AcceptPriceListModel *model = [AcceptPriceListModel yy_modelWithJSON:dic];
                [arrM addObject:model];
                [arrMImage addObject:model.bizPortrait];
            }
            
            self.arrM = arrM;
            
            self.BziInfo.model = self.arrM[0];
            
            [self.BizeScr addScrImage1:arrMImage];
            
            
        }else{
            [QMUITips showInfo:x.errorInfo];
        }
    }];
}
@end
